﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace BaseSimpleAPI
{
    public class StatusDataPrompt
    {
        private StatusDataPrompt() { }

        private const string LANGUAGE_FILE_PATH = "./Languages";

        private StatusDataPrompt(string key)
        {
            // 根据名称加载合适的数据
            string file_path = string.Format("{0}/{1}.xml", LANGUAGE_HEADER_KEY, key);

            if (!File.Exists(file_path))
                return;

            XmlDocument document = new XmlDocument();
            document.Load(file_path);

            XmlNodeList list = document.SelectNodes("Root/Item");
            foreach (XmlNode item in list)
            {
                var item_key = item.Attributes["Key"].Value;
                var item_value = item.Attributes["Value"].Value;

                FieldInfo info = GetType().GetField(item_key);
                if (info == null)
                    continue;

                info.SetValue(this, item_value);
            }
        }

        private static Dictionary<string, StatusDataPrompt> Mapping = new Dictionary<string, StatusDataPrompt>();

        /// <summary>
        /// 获取提示数据
        /// </summary>
        /// <returns></returns>
        public static StatusDataPrompt GetStatusDataPrompt()
        {
            var key = GetAppropriateKey();
            if (!Mapping.ContainsKey(key)) lock (Mapping) if (!Mapping.ContainsKey(key))
                        Mapping[key] = new StatusDataPrompt(key);

            return Mapping[key];
        }

        /// <summary>
        /// 语言使用的Key
        /// </summary>
        private const string LANGUAGE_HEADER_KEY = "language";

        /// <summary>
        /// 获取合适的语言Key
        /// </summary>
        /// <returns></returns>
        private static string GetAppropriateKey()
        {
            // UDPPackage header = NetCallContext.Current.RequestUDPPackage.Header;
            //if (header.ContainsKey(LANGUAGE_HEADER_KEY))
            //    return header[LANGUAGE_HEADER_KEY];

            return "Chinese";
        }

        // 通用
        public string NoPermissions { get; private set; } = "没有权限";
        public string UserNameOrPasswordError { get; private set; } = "用户名或者密码错误";
        public string NotFindObjects { get; private set; } = "找不到对象";
        public string UpdateError { get; private set; } = "更新错误";
        public string RemoveError { get; private set; } = "删除错误";

        public string Success { get; private set; } = "成功";
        public string Error { get; private set; } = "错误";

        public string ModelCheckFail { get; private set; } = "传入数据不符合标准";

        // 用户相关
        public string ExistingUser { get; private set; } = "已经存在的用户";
        public string LogoutError { get; private set; } = "登出失败";
        public string LogoutSuccess { get; private set; } = "登出成功";
    }

    public class StatusData
    {
        public StatusDataStatus Status { get; set; }

        public string Message { set; get; }

        protected object _data;

        public object GetData() { return _data; }

        public T GetData<T>() { return (T)_data; }

        public static StatusData Success(string message = null)
        {
            return new StatusData() { Status = StatusDataStatus.Success, Message = message };
        }

        public static StatusData Error(string message = null)
        {
            return new StatusData { Status = StatusDataStatus.Error, Message = message };
        }

        public static StatusData Warning(string message = null)
        {
            return new StatusData { Status = StatusDataStatus.Warning, Message = message };
        }

        public static StatusData Error(object existingUser)
        {
            throw new NotImplementedException();
        }

        public static StatusData Success(object success)
        {
            throw new NotImplementedException();
        }
    }

    public class StatusObjectData : StatusData
    {
        public Object Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public static StatusObjectData Success(object data)
        {
            return new StatusObjectData { Status = StatusDataStatus.Success, Data = data };
        }
    }

    public class StatusData<T> : StatusData
    {
        public T Data
        {
            get { return (T)_data; }
            set { _data = value; }
        }

        public static StatusData<T> Success(T data)
        {
            return new StatusData<T> { Status = StatusDataStatus.Success, Data = data };
        }
    }
}
